package main

//反射调用方法
import (
	"log"
	"reflect"
)

type Person struct {
	Name   string
	Age    int
	Gender string
}

func (p Person) ReflectCallFunWithArgs(name string, age int) {
	log.Printf("[调用带参数的方法][args.name:%s][args.age:%d][p.name:%s][p.age:%d]",
		name,
		age,
		p.Name,
		p.Age,
	)
}

func (p Person) ReflectCallFuncWithNoArgs() {
	log.Printf("[调用不带参数的方法]")
}

func main() {
	p1 := Person{
		Name:   "lwq",
		Age:    18,
		Gender: "man",
	}

	getValue := reflect.ValueOf(p1) //获取反射类型对象
	//带参数调用
	methodValue := getValue.MethodByName("ReflectCallFunWithArgs") //注册
	args := []reflect.Value{reflect.ValueOf("lll"), reflect.ValueOf(19)}
	methodValue.Call(args)
	//不带参数调用
	methodValue = getValue.MethodByName("ReflectCallFuncWithNoArgs")
	args = make([]reflect.Value, 0)
	methodValue.Call(args)
}
